package xx.rocketmq.util;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.*;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Classname MqUtil
 * @Description TODO
 * @Date 2023/11/21 13:40
 * @Created by wangxianlin
 */
public class ProducerUtil {

    public static void main(String[] args) throws UnsupportedEncodingException, MQClientException, MQBrokerException, RemotingException, InterruptedException {
        //1.创建一个发送消息的对象Producer
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2.设定发送的命名服务器地址
        producer.setNamesrvAddr("106.14.206.239:9876");
        //3.1启动发送的服务
        producer.start();
        //4.创建要发送的消息对象,指定topic，指定内容body
        Message msg = new Message("topic_delay","延时消息 hello rocketmq".getBytes(StandardCharsets.UTF_8));

        //设置当前消息的延时效果
        msg.setDelayTimeLevel(2);

        //4.2发送消息
        SendResult sendResult = producer.send(msg);
        System.out.println("结果 is "+ sendResult);
        //5.关闭连接
        producer.shutdown();
    }
}
